(function () {
    "use strict";
    angular.module("bigScreen", ['commons', 'layout','panels', 'ngAnimate', 'templates'])
        .config(function ($sceProvider, $logProvider, $httpProvider) {
            $sceProvider.enabled(false);
            $logProvider.debugEnabled(false);
            // 默认禁止浏览器缓存
            $httpProvider.defaults.headers.common['Cache-Control'] = 'no-cache';
            $httpProvider.defaults.headers.common['If-Modified-Since'] = '0';// no-cache setting for IE
        }).controller('screenController', function ($scope) {

        }).value("urlPrefix", "../")
        .value("titlePrefix", "BOMC门户 -大屏展现 ")
        .value("physicalScreenId", (function () {
            var searchStr = window.location.search.length > 0 ? window.location.search.substring(1) : "";
            var items = searchStr.length > 0 ? searchStr.split("&") : [];
            for (var i = 0; i < items.length; i++) {
                if (items[i].split("=")[0] == "physicalScreenId") {
                    return items[i].split("=")[1];
                }
            }
        })())
        .directive("screenContainer", function ($http, Promise, loadConf, urlPrefix, physicalScreenId, $timeout, $window) {
            return {
                scope: {},
                link: function (scope, elem) {
                    scope.isArray = angular.isArray;
                    Promise.all({
                        logicalScreens: $http.get(urlPrefix + "/ibnms/bigscreen/queryLogicalScreenByPhysicalId.action?physicalScreenId=!!" +
                        physicalScreenId).then(function (resp) {
                            return resp.data.rows;
                        })
                    }).then(function (results) {
                        scope.logicalScreens = results.logicalScreens;
                        if (scope.logicalScreens.length > 0) {
                            scope.logicalScreenId = scope.logicalScreens[0].logicalScreenId;
                        }
                        if (scope.logicalScreens.length > 1) {
                            var index = 1;
                            var switchId = function (seconds) {
                                $timeout(function () {
                                    var currentScreen = scope.logicalScreens[index];
                                    if (index < scope.logicalScreens.length - 1) {
                                        index++;
                                    } else {
                                        index = 0;
                                    }
                                    scope.logicalScreenId = currentScreen.logicalScreenId;
                                    switchId(currentScreen.showSeconds);
                                    $timeout(function(){
                                        angular.element($window).resize();
                                    },1);
                                }, seconds * 1000);
                            };
                            switchId(scope.logicalScreens[0].showSeconds);
                        }
                    });
                },
                replace: false,
                templateUrl: 'app/big-screen/big-screen.html'
            }
        });
})();